package presentation;


import phasebookws.ESBWS;
import phasebookws.IFacade;
import phasebookws.PhasebookToken;
import phasebookws.InvalidAuthenticationException_Exception;
import phasebookws.Gender;

public class PhaseBook {
	
	public static IFacade GetFacade() {

		return (new ESBWS()).getFacadePort();

	}

	public static Boolean ResetPassword(PhasebookToken token, String userName) {

		try {
			IFacade facadeBean = (new ESBWS()).getFacadePort();

			return facadeBean.resetPassword(token, userName);
			
		} catch (InvalidAuthenticationException_Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} 

	}

	public static Boolean Register(String userName, String password,
			String name, String age, String gender, String locality) {

		try {
			IFacade facadeBean = (new ESBWS()).getFacadePort();

			if (facadeBean.registerUser(userName, password)) {				

				PhasebookToken token = facadeBean.authenticate(userName, password);
				facadeBean.updateName(token, name);

				try {
					int intAge = Integer.parseInt(age);
					facadeBean.updateAge(token, intAge);
				} catch (NumberFormatException exc) {
					System.out.println("Age is not an Integer");
				}

				if (gender.equals("Male"))
					facadeBean.updateGender(token, Gender.MALE);
				else if (gender.equals("Female"))
					facadeBean.updateGender(token, Gender.FEMALE);
				else
					facadeBean.updateGender(token, null); // rebenta!

				facadeBean.updateCity(token, locality);

				return true;
			}

			return false;

		} catch (InvalidAuthenticationException_Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}

	}

	public static Boolean UpdateProfile(IFacade facadeBean, PhasebookToken token, String name,
			String age, String locality, String gender) {

		try {
			facadeBean.updateName(token, name);

			try {
				int intAge = Integer.parseInt(age);
				//facadeBean.updateAge(uid, intAge);
				System.out.println("<<<<<<<<<<<<<<<<<<<<<<< TESTING >>>>>>>>>>>>>>>>>>>>>>>>>");
				facadeBean.updateAge(token, intAge);
			} catch (NumberFormatException exc) {
				System.out.println("Age is not an Integer");
			} catch (InvalidAuthenticationException_Exception e) {
				System.out.println("Age was not updated");
				e.printStackTrace();
			}

			facadeBean.updateCity(token, locality);

			if (gender.equals("Male"))
				facadeBean.updateGender(token, Gender.MALE);
			else if (gender.equals("Female"))
				facadeBean.updateGender(token, Gender.FEMALE);
			else
				facadeBean.updateGender(token, null);

			return true;

		} catch (InvalidAuthenticationException_Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}
}
